package com.api.service.v1;


import com.admin.entity.Station;
import com.admin.entity.Terminal;
import com.admin.mapper.StationMapper;
import com.aliyun.OssUpload;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Service
public class WelcomeApiService {

    @Value("${SERVER}")
    private String ECS_SERVER;

    @Autowired
    private OssUpload ossUpload;

    @Value("${oss}")
    private Boolean oss;

    @Autowired
    private  TerminalApiService terminalApiService;

    @Autowired
    private StationMapper stationMapper;

    @Autowired
    private RestTemplate restTemplate;
    /**
     * 获取旅客的PMS姓名,动态欢迎词
     * @param mac
     * @return
     */
    public String getPmsName(String mac, String id, String room) {
        Terminal t = terminalApiService.findTerminalByMac(mac);
        /*if (StringUtil.isNotNull(id))
        {
            Station station = stationMapper.selectById(id);
            if (station!=null)
            {
                String pmsUrl = station.getPms();
                if (pmsUrl!=null)
                {
                    pmsUrl = pmsUrl+ mac;
                    String response = restTemplate.getForObject(pmsUrl,String.class);
                    if (StringUtil.isNotNull(response))
                    {


                    }
                }
            }

        }*/
        if (t==null) {
            return "贵宾";
        }
        String name = t.getTerminalPmsName();
        if (StringUtil.isNull(name)){
            return "贵宾";
        }

        return name;

    }

    public List getServers(String mac) {
        Station station = terminalApiService.findStationByMac(mac);
        List list = new ArrayList();
        if (station != null) {
            String localServer = station.getLocalServerIp();
            if (localServer==null){
                localServer = station.getStationLocalServerIp();
            }
            if (localServer != null && localServer.length()>0) {
                if (localServer.startsWith("http://") == false) {
                    localServer = "http://" + localServer;
                }
              /*  if (localServer.indexOf("8080") == -1) {
                    localServer = localServer + ":8080";
                }*/
                if (localServer.indexOf("local-server") == -1) {
                    localServer = localServer + "/local-server";
                }
                list.add(localServer);
            }
        }

        if (ossUpload.getUrl() != null && oss) {
            list.add(ossUpload.getUrl());
        }

        list.add(ECS_SERVER);
        return list;
    }
}
